home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / lib / iceweasel / components / nsBrowserContentHandler.js < prev    next >
Encoding:
Text File  |  2013-01-09  |  29.7 KB  |  833 lines

  1. //@line 38 "/tmp/buildd/iceweasel-10.0.12esr/browser/components/nsBrowserContentHandler.js"
  2.  
  3. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  4. Components.utils.import("resource://gre/modules/Services.jsm");
  5.  
  6. const nsISupports            = Components.interfaces.nsISupports;
  7.  
  8. const nsIBrowserDOMWindow    = Components.interfaces.nsIBrowserDOMWindow;
  9. const nsIBrowserHandler      = Components.interfaces.nsIBrowserHandler;
  10. const nsIBrowserHistory      = Components.interfaces.nsIBrowserHistory;
  11. const nsIChannel             = Components.interfaces.nsIChannel;
  12. const nsICommandLine         = Components.interfaces.nsICommandLine;
  13. const nsICommandLineHandler  = Components.interfaces.nsICommandLineHandler;
  14. const nsIContentHandler      = Components.interfaces.nsIContentHandler;
  15. const nsIDocShellTreeItem    = Components.interfaces.nsIDocShellTreeItem;
  16. const nsIDOMChromeWindow     = Components.interfaces.nsIDOMChromeWindow;
  17. const nsIDOMWindow           = Components.interfaces.nsIDOMWindow;
  18. const nsIFileURL             = Components.interfaces.nsIFileURL;
  19. const nsIHttpProtocolHandler = Components.interfaces.nsIHttpProtocolHandler;
  20. const nsIInterfaceRequestor  = Components.interfaces.nsIInterfaceRequestor;
  21. const nsINetUtil             = Components.interfaces.nsINetUtil;
  22. const nsIPrefBranch          = Components.interfaces.nsIPrefBranch;
  23. const nsIPrefLocalizedString = Components.interfaces.nsIPrefLocalizedString;
  24. const nsISupportsString      = Components.interfaces.nsISupportsString;
  25. const nsIURIFixup            = Components.interfaces.nsIURIFixup;
  26. const nsIWebNavigation       = Components.interfaces.nsIWebNavigation;
  27. const nsIWindowMediator      = Components.interfaces.nsIWindowMediator;
  28. const nsIWindowWatcher       = Components.interfaces.nsIWindowWatcher;
  29. const nsIWebNavigationInfo   = Components.interfaces.nsIWebNavigationInfo;
  30. const nsIBrowserSearchService = Components.interfaces.nsIBrowserSearchService;
  31. const nsICommandLineValidator = Components.interfaces.nsICommandLineValidator;
  32. const nsIXULAppInfo          = Components.interfaces.nsIXULAppInfo;
  33.  
  34. const NS_BINDING_ABORTED = Components.results.NS_BINDING_ABORTED;
  35. const NS_ERROR_WONT_HANDLE_CONTENT = 0x805d0001;
  36. const NS_ERROR_ABORT = Components.results.NS_ERROR_ABORT;
  37.  
  38. const URI_INHERITS_SECURITY_CONTEXT = nsIHttpProtocolHandler
  39.                                         .URI_INHERITS_SECURITY_CONTEXT;
  40.  
  41. function shouldLoadURI(aURI) {
  42.   if (aURI && !aURI.schemeIs("chrome"))
  43.     return true;
  44.  
  45.   dump("*** Preventing external load of chrome: URI into browser window\n");
  46.   dump("    Use -chrome <uri> instead\n");
  47.   return false;
  48. }
  49.  
  50. function resolveURIInternal(aCmdLine, aArgument) {
  51.   var uri = aCmdLine.resolveURI(aArgument);
  52.  
  53.   if (!(uri instanceof nsIFileURL)) {
  54.     return uri;
  55.   }
  56.  
  57.   try {
  58.     if (uri.file.exists())
  59.       return uri;
  60.   }
  61.   catch (e) {
  62.     Components.utils.reportError(e);
  63.   }
  64.  
  65.   // We have interpreted the argument as a relative file URI, but the file
  66.   // doesn't exist. Try URI fixup heuristics: see bug 290782.
  67.  
  68.   try {
  69.     var urifixup = Components.classes["@mozilla.org/docshell/urifixup;1"]
  70.                              .getService(nsIURIFixup);
  71.  
  72.     uri = urifixup.createFixupURI(aArgument, 0);
  73.   }
  74.   catch (e) {
  75.     Components.utils.reportError(e);
  76.   }
  77.  
  78.   return uri;
  79. }
  80.  
  81. const OVERRIDE_NONE        = 0;
  82. const OVERRIDE_NEW_PROFILE = 1;
  83. const OVERRIDE_NEW_MSTONE  = 2;
  84. const OVERRIDE_NEW_BUILD_ID = 3;
  85. /**
  86.  * Determines whether a home page override is needed.
  87.  * Returns:
  88.  *  OVERRIDE_NEW_PROFILE if this is the first run with a new profile.
  89.  *  OVERRIDE_NEW_MSTONE if this is the first run with a build with a different
  90.  *                      Gecko milestone (i.e. right after an upgrade).
  91.  *  OVERRIDE_NEW_BUILD_ID if this is the first run with a new build ID of the
  92.  *                        same Gecko milestone (i.e. after a nightly upgrade).
  93.  *  OVERRIDE_NONE otherwise.
  94.  */
  95. function needHomepageOverride(prefb) {
  96.   var savedmstone = null;
  97.   try {
  98.     savedmstone = prefb.getCharPref("browser.startup.homepage_override.mstone");
  99.   } catch (e) {}
  100.  
  101.   if (savedmstone == "ignore")
  102.     return OVERRIDE_NONE;
  103.  
  104.   var mstone = Components.classes["@mozilla.org/network/protocol;1?name=http"]
  105.                          .getService(nsIHttpProtocolHandler).misc;
  106.  
  107.   var savedBuildID = null;
  108.   try {
  109.     savedBuildID = prefb.getCharPref("browser.startup.homepage_override.buildID");
  110.   } catch (e) {}
  111.  
  112.   var buildID =  Components.classes["@mozilla.org/xre/app-info;1"]
  113.                            .getService(nsIXULAppInfo).platformBuildID;
  114.  
  115.   if (mstone != savedmstone) {
  116.     // Bug 462254. Previous releases had a default pref to suppress the EULA
  117.     // agreement if the platform's installer had already shown one. Now with
  118.     // about:rights we've removed the EULA stuff and default pref, but we need
  119.     // a way to make existing profiles retain the default that we removed.
  120.     if (savedmstone)
  121.       prefb.setBoolPref("browser.rights.3.shown", true);
  122.     
  123.     prefb.setCharPref("browser.startup.homepage_override.mstone", mstone);
  124.     prefb.setCharPref("browser.startup.homepage_override.buildID", buildID);
  125.     return (savedmstone ? OVERRIDE_NEW_MSTONE : OVERRIDE_NEW_PROFILE);
  126.   }
  127.  
  128.   if (buildID != savedBuildID) {
  129.     prefb.setCharPref("browser.startup.homepage_override.buildID", buildID);
  130.     return OVERRIDE_NEW_BUILD_ID;
  131.   }
  132.  
  133.   return OVERRIDE_NONE;
  134. }
  135.  
  136. /**
  137.  * Gets the override page for the first run after the application has been
  138.  * updated.
  139.  * @param  defaultOverridePage
  140.  *         The default override page.
  141.  * @return The override page.
  142.  */
  143. function getPostUpdateOverridePage(defaultOverridePage) {
  144.   var um = Components.classes["@mozilla.org/updates/update-manager;1"]
  145.                      .getService(Components.interfaces.nsIUpdateManager);
  146.   try {
  147.     // If the updates.xml file is deleted then getUpdateAt will throw.
  148.     var update = um.getUpdateAt(0)
  149.                    .QueryInterface(Components.interfaces.nsIPropertyBag);
  150.   } catch (e) {
  151.     // This should never happen.
  152.     Components.utils.reportError("Unable to find update: " + e);
  153.     return defaultOverridePage;
  154.   }
  155.  
  156.   let actions = update.getProperty("actions");
  157.   // When the update doesn't specify actions fallback to the original behavior
  158.   // of displaying the default override page.
  159.   if (!actions)
  160.     return defaultOverridePage;
  161.  
  162.   // The existence of silent or the non-existence of showURL in the actions both
  163.   // mean that an override page should not be displayed.
  164.   if (actions.indexOf("silent") != -1 || actions.indexOf("showURL") == -1)
  165.     return "";
  166.  
  167.   return update.getProperty("openURL") || defaultOverridePage;
  168. }
  169.  
  170. // Copies a pref override file into the user's profile pref-override folder,
  171. // and then tells the pref service to reload its default prefs.
  172. function copyPrefOverride() {
  173.   try {
  174.     var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"]
  175.                                 .getService(Components.interfaces.nsIProperties);
  176.     const NS_APP_EXISTING_PREF_OVERRIDE = "ExistingPrefOverride";
  177.     var prefOverride = fileLocator.get(NS_APP_EXISTING_PREF_OVERRIDE,
  178.                                        Components.interfaces.nsIFile);
  179.     if (!prefOverride.exists())
  180.       return; // nothing to do
  181.  
  182.     const NS_APP_PREFS_OVERRIDE_DIR     = "PrefDOverride";
  183.     var prefOverridesDir = fileLocator.get(NS_APP_PREFS_OVERRIDE_DIR,
  184.                                            Components.interfaces.nsIFile);
  185.  
  186.     // Check for any existing pref overrides, and remove them if present
  187.     var existingPrefOverridesFile = prefOverridesDir.clone();
  188.     existingPrefOverridesFile.append(prefOverride.leafName);
  189.     if (existingPrefOverridesFile.exists())
  190.       existingPrefOverridesFile.remove(false);
  191.  
  192.     prefOverride.copyTo(prefOverridesDir, null);
  193.  
  194.     // Now that we've installed the new-profile pref override file,
  195.     // re-read the default prefs.
  196.     var prefSvcObs = Components.classes["@mozilla.org/preferences-service;1"]
  197.                                .getService(Components.interfaces.nsIObserver);
  198.     prefSvcObs.observe(null, "reload-default-prefs", null);
  199.   } catch (ex) {
  200.     Components.utils.reportError(ex);
  201.   }
  202. }
  203.  
  204. // Flag used to indicate that the arguments to openWindow can be passed directly.
  205. const NO_EXTERNAL_URIS = 1;
  206.  
  207. function openWindow(parent, url, target, features, args, noExternalArgs) {
  208.   var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  209.                          .getService(nsIWindowWatcher);
  210.  
  211.   if (noExternalArgs == NO_EXTERNAL_URIS) {
  212.     // Just pass in the defaultArgs directly
  213.     var argstring;
  214.     if (args) {
  215.       argstring = Components.classes["@mozilla.org/supports-string;1"]
  216.                             .createInstance(nsISupportsString);
  217.       argstring.data = args;
  218.     }
  219.  
  220.     return wwatch.openWindow(parent, url, target, features, argstring);
  221.   }
  222.   
  223.   // Pass an array to avoid the browser "|"-splitting behavior.
  224.   var argArray = Components.classes["@mozilla.org/supports-array;1"]
  225.                     .createInstance(Components.interfaces.nsISupportsArray);
  226.  
  227.   // add args to the arguments array
  228.   var stringArgs = null;
  229.   if (args instanceof Array) // array
  230.     stringArgs = args;
  231.   else if (args) // string
  232.     stringArgs = [args];
  233.  
  234.   if (stringArgs) {
  235.     // put the URIs into argArray
  236.     var uriArray = Components.classes["@mozilla.org/supports-array;1"]
  237.                        .createInstance(Components.interfaces.nsISupportsArray);
  238.     stringArgs.forEach(function (uri) {
  239.       var sstring = Components.classes["@mozilla.org/supports-string;1"]
  240.                               .createInstance(nsISupportsString);
  241.       sstring.data = uri;
  242.       uriArray.AppendElement(sstring);
  243.     });
  244.     argArray.AppendElement(uriArray);
  245.   } else {
  246.     argArray.AppendElement(null);
  247.   }
  248.  
  249.   // Pass these as null to ensure that we always trigger the "single URL"
  250.   // behavior in browser.js's BrowserStartup (which handles the window
  251.   // arguments)
  252.   argArray.AppendElement(null); // charset
  253.   argArray.AppendElement(null); // referer
  254.   argArray.AppendElement(null); // postData
  255.   argArray.AppendElement(null); // allowThirdPartyFixup
  256.  
  257.   return wwatch.openWindow(parent, url, target, features, argArray);
  258. }
  259.  
  260. function openPreferences() {
  261.   var features = "chrome,titlebar,toolbar,centerscreen,dialog=no";
  262.   var url = "chrome://browser/content/preferences/preferences.xul";
  263.  
  264.   var win = getMostRecentWindow("Browser:Preferences");
  265.   if (win) {
  266.     win.focus();
  267.   } else {
  268.     openWindow(null, url, "_blank", features);
  269.   }
  270. }
  271.  
  272. function getMostRecentWindow(aType) {
  273.   var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  274.                      .getService(nsIWindowMediator);
  275.   return wm.getMostRecentWindow(aType);
  276. }
  277.  
  278. // this returns the most recent non-popup browser window
  279. function getMostRecentBrowserWindow() {
  280.   var browserGlue = Components.classes["@mozilla.org/browser/browserglue;1"]
  281.                               .getService(Components.interfaces.nsIBrowserGlue);
  282.   return browserGlue.getMostRecentBrowserWindow();
  283. }
  284.  
  285. function doSearch(searchTerm, cmdLine) {
  286.   var ss = Components.classes["@mozilla.org/browser/search-service;1"]
  287.                      .getService(nsIBrowserSearchService);
  288.  
  289.   var submission = ss.defaultEngine.getSubmission(searchTerm);
  290.  
  291.   // fill our nsISupportsArray with uri-as-wstring, null, null, postData
  292.   var sa = Components.classes["@mozilla.org/supports-array;1"]
  293.                      .createInstance(Components.interfaces.nsISupportsArray);
  294.  
  295.   var wuri = Components.classes["@mozilla.org/supports-string;1"]
  296.                        .createInstance(Components.interfaces.nsISupportsString);
  297.   wuri.data = submission.uri.spec;
  298.  
  299.   sa.AppendElement(wuri);
  300.   sa.AppendElement(null);
  301.   sa.AppendElement(null);
  302.   sa.AppendElement(submission.postData);
  303.  
  304.   // XXXbsmedberg: use handURIToExistingBrowser to obey tabbed-browsing
  305.   // preferences, but need nsIBrowserDOMWindow extensions
  306.  
  307.   var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  308.                          .getService(nsIWindowWatcher);
  309.  
  310.   return wwatch.openWindow(null, gBrowserContentHandler.chromeURL,
  311.                            "_blank",
  312.                            "chrome,dialog=no,all" +
  313.                            gBrowserContentHandler.getFeatures(cmdLine),
  314.                            sa);
  315. }
  316.  
  317. function nsBrowserContentHandler() {
  318. }
  319. nsBrowserContentHandler.prototype = {
  320.   classID: Components.ID("{5d0ce354-df01-421a-83fb-7ead0990c24e}"),
  321.  
  322.   _xpcom_factory: {
  323.     createInstance: function bch_factory_ci(outer, iid) {
  324.       if (outer)
  325.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  326.       return gBrowserContentHandler.QueryInterface(iid);
  327.     }
  328.   },
  329.  
  330.   /* helper functions */
  331.  
  332.   mChromeURL : null,
  333.  
  334.   get chromeURL() {
  335.     if (this.mChromeURL) {
  336.       return this.mChromeURL;
  337.     }
  338.  
  339.     var prefb = Components.classes["@mozilla.org/preferences-service;1"]
  340.                           .getService(nsIPrefBranch);
  341.     this.mChromeURL = prefb.getCharPref("browser.chromeURL");
  342.  
  343.     return this.mChromeURL;
  344.   },
  345.  
  346.   /* nsISupports */
  347.   QueryInterface : XPCOMUtils.generateQI([nsICommandLineHandler,
  348.                                           nsIBrowserHandler,
  349.                                           nsIContentHandler,
  350.                                           nsICommandLineValidator]),
  351.  
  352.   /* nsICommandLineHandler */
  353.   handle : function bch_handle(cmdLine) {
  354.     if (cmdLine.handleFlag("browser", false)) {
  355.       // Passing defaultArgs, so use NO_EXTERNAL_URIS
  356.       openWindow(null, this.chromeURL, "_blank",
  357.                  "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  358.                  this.defaultArgs, NO_EXTERNAL_URIS);
  359.       cmdLine.preventDefault = true;
  360.     }
  361.  
  362.     try {
  363.       var remoteCommand = cmdLine.handleFlagWithParam("remote", true);
  364.     }
  365.     catch (e) {
  366.       throw NS_ERROR_ABORT;
  367.     }
  368.  
  369.     if (remoteCommand != null) {
  370.       try {
  371.         var a = /^\s*(\w+)\(([^\)]*)\)\s*$/.exec(remoteCommand);
  372.         var remoteVerb;
  373.         if (a) {
  374.           remoteVerb = a[1].toLowerCase();
  375.           var remoteParams = [];
  376.           var sepIndex = a[2].lastIndexOf(",");
  377.           if (sepIndex == -1)
  378.             remoteParams[0] = a[2];
  379.           else {
  380.             remoteParams[0] = a[2].substring(0, sepIndex);
  381.             remoteParams[1] = a[2].substring(sepIndex + 1);
  382.           }
  383.         }
  384.  
  385.         switch (remoteVerb) {
  386.         case "openurl":
  387.         case "openfile":
  388.           // openURL(<url>)
  389.           // openURL(<url>,new-window)
  390.           // openURL(<url>,new-tab)
  391.  
  392.           // First param is the URL, second param (if present) is the "target"
  393.           // (tab, window)
  394.           var url = remoteParams[0];
  395.           var target = nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW;
  396.           if (remoteParams[1]) {
  397.             var targetParam = remoteParams[1].toLowerCase()
  398.                                              .replace(/^\s*|\s*$/g, "");
  399.             if (targetParam == "new-tab")
  400.               target = nsIBrowserDOMWindow.OPEN_NEWTAB;
  401.             else if (targetParam == "new-window")
  402.               target = nsIBrowserDOMWindow.OPEN_NEWWINDOW;
  403.             else {
  404.               // The "target" param isn't one of our supported values, so
  405.               // assume it's part of a URL that contains commas.
  406.               url += "," + remoteParams[1];
  407.             }
  408.           }
  409.  
  410.           var uri = resolveURIInternal(cmdLine, url);
  411.           handURIToExistingBrowser(uri, target, cmdLine);
  412.           break;
  413.  
  414.         case "xfedocommand":
  415.           // xfeDoCommand(openBrowser)
  416.           if (remoteParams[0].toLowerCase() != "openbrowser")
  417.             throw NS_ERROR_ABORT;
  418.  
  419.           // Passing defaultArgs, so use NO_EXTERNAL_URIS
  420.           openWindow(null, this.chromeURL, "_blank",
  421.                      "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  422.                      this.defaultArgs, NO_EXTERNAL_URIS);
  423.           break;
  424.  
  425.         default:
  426.           // Somebody sent us a remote command we don't know how to process:
  427.           // just abort.
  428.           throw "Unknown remote command.";
  429.         }
  430.  
  431.         cmdLine.preventDefault = true;
  432.       }
  433.       catch (e) {
  434.         Components.utils.reportError(e);
  435.         // If we had a -remote flag but failed to process it, throw
  436.         // NS_ERROR_ABORT so that the xremote code knows to return a failure
  437.         // back to the handling code.
  438.         throw NS_ERROR_ABORT;
  439.       }
  440.     }
  441.  
  442.     var uriparam;
  443.     try {
  444.       while ((uriparam = cmdLine.handleFlagWithParam("new-window", false))) {
  445.         var uri = resolveURIInternal(cmdLine, uriparam);
  446.         if (!shouldLoadURI(uri))
  447.           continue;
  448.         openWindow(null, this.chromeURL, "_blank",
  449.                    "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  450.                    uri.spec);
  451.         cmdLine.preventDefault = true;
  452.       }
  453.     }
  454.     catch (e) {
  455.       Components.utils.reportError(e);
  456.     }
  457.  
  458.     try {
  459.       while ((uriparam = cmdLine.handleFlagWithParam("new-tab", false))) {
  460.         var uri = resolveURIInternal(cmdLine, uriparam);
  461.         handURIToExistingBrowser(uri, nsIBrowserDOMWindow.OPEN_NEWTAB, cmdLine);
  462.         cmdLine.preventDefault = true;
  463.       }
  464.     }
  465.     catch (e) {
  466.       Components.utils.reportError(e);
  467.     }
  468.  
  469.     var chromeParam = cmdLine.handleFlagWithParam("chrome", false);
  470.     if (chromeParam) {
  471.  
  472.       // Handle the old preference dialog URL separately (bug 285416)
  473.       if (chromeParam == "chrome://browser/content/pref/pref.xul") {
  474.         openPreferences();
  475.         cmdLine.preventDefault = true;
  476.       } else try {
  477.         // only load URIs which do not inherit chrome privs
  478.         var features = "chrome,dialog=no,all" + this.getFeatures(cmdLine);
  479.         var uri = resolveURIInternal(cmdLine, chromeParam);
  480.         var netutil = Components.classes["@mozilla.org/network/util;1"]
  481.                                 .getService(nsINetUtil);
  482.         if (!netutil.URIChainHasFlags(uri, URI_INHERITS_SECURITY_CONTEXT)) {
  483.           openWindow(null, uri.spec, "_blank", features);
  484.           cmdLine.preventDefault = true;
  485.         }
  486.       }
  487.       catch (e) {
  488.         Components.utils.reportError(e);
  489.       }
  490.     }
  491.     if (cmdLine.handleFlag("preferences", false)) {
  492.       openPreferences();
  493.       cmdLine.preventDefault = true;
  494.     }
  495.     if (cmdLine.handleFlag("silent", false))
  496.       cmdLine.preventDefault = true;
  497.     if (cmdLine.findFlag("private-toggle", false) >= 0)
  498.       cmdLine.preventDefault = true;
  499.  
  500.     var searchParam = cmdLine.handleFlagWithParam("search", false);
  501.     if (searchParam) {
  502.       doSearch(searchParam, cmdLine);
  503.       cmdLine.preventDefault = true;
  504.     }
  505.  
  506.     var fileParam = cmdLine.handleFlagWithParam("file", false);
  507.     if (fileParam) {
  508.       var file = cmdLine.resolveFile(fileParam);
  509.       var ios = Components.classes["@mozilla.org/network/io-service;1"]
  510.                           .getService(Components.interfaces.nsIIOService);
  511.       var uri = ios.newFileURI(file);
  512.       openWindow(null, this.chromeURL, "_blank", 
  513.                  "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  514.                  uri.spec);
  515.       cmdLine.preventDefault = true;
  516.     }
  517.  
  518. //@line 567 "/tmp/buildd/iceweasel-10.0.12esr/browser/components/nsBrowserContentHandler.js"
  519.   },
  520.  
  521.   helpInfo : "  -browser           Open a browser window.\n" +
  522.              "  -new-window  <url> Open <url> in a new window.\n" +
  523.              "  -new-tab     <url> Open <url> in a new tab.\n" +
  524. //@line 575 "/tmp/buildd/iceweasel-10.0.12esr/browser/components/nsBrowserContentHandler.js"
  525.              "  -preferences       Open Preferences dialog.\n" +
  526. //@line 577 "/tmp/buildd/iceweasel-10.0.12esr/browser/components/nsBrowserContentHandler.js"
  527.              "  -search     <term> Search <term> with your default search engine.\n",
  528.  
  529.   /* nsIBrowserHandler */
  530.  
  531.   get defaultArgs() {
  532.     var prefb = Components.classes["@mozilla.org/preferences-service;1"]
  533.                           .getService(nsIPrefBranch);
  534.  
  535.     var overridePage = "";
  536.     var haveUpdateSession = false;
  537.     try {
  538.       let override = needHomepageOverride(prefb);
  539.       if (override != OVERRIDE_NONE) {
  540.         // Setup the default search engine to about:home page.
  541.         AboutHomeUtils.loadDefaultSearchEngine();
  542.         AboutHomeUtils.loadSnippetsURL();
  543.  
  544.         switch (override) {
  545.           case OVERRIDE_NEW_PROFILE:
  546.             // New profile.
  547.             overridePage = Services.urlFormatter.formatURLPref("startup.homepage_welcome_url");
  548.             break;
  549.           case OVERRIDE_NEW_MSTONE:
  550.             // Existing profile, new milestone build.
  551.             copyPrefOverride();
  552.  
  553.             // Check whether we have a session to restore. If we do, we assume
  554.             // that this is an "update" session.
  555.             var ss = Components.classes["@mozilla.org/browser/sessionstartup;1"]
  556.                                .getService(Components.interfaces.nsISessionStartup);
  557.             haveUpdateSession = ss.doRestore();
  558.             overridePage = Services.urlFormatter.formatURLPref("startup.homepage_override_url");
  559.             if (prefb.prefHasUserValue("app.update.postupdate"))
  560.               overridePage = getPostUpdateOverridePage(overridePage);
  561.             break;
  562.         }
  563.       }
  564.       else {
  565.         // No need to override homepage, but update snippets url if the pref has
  566.         // been manually changed.
  567.         if (Services.prefs.prefHasUserValue(AboutHomeUtils.SNIPPETS_URL_PREF)) {
  568.           AboutHomeUtils.loadSnippetsURL();
  569.         }
  570.       }
  571.     } catch (ex) {}
  572.  
  573.     // formatURLPref might return "about:blank" if getting the pref fails
  574.     if (overridePage == "about:blank")
  575.       overridePage = "";
  576.  
  577.     var startPage = "";
  578.     try {
  579.       var choice = prefb.getIntPref("browser.startup.page");
  580.       if (choice == 1 || choice == 3)
  581.         startPage = this.startPage;
  582.     } catch (e) {
  583.       Components.utils.reportError(e);
  584.     }
  585.  
  586.     if (startPage == "about:blank")
  587.       startPage = "";
  588.  
  589.     // Only show the startPage if we're not restoring an update session.
  590.     if (overridePage && startPage && !haveUpdateSession)
  591.       return overridePage + "|" + startPage;
  592.  
  593.     return overridePage || startPage || "about:blank";
  594.   },
  595.  
  596.   get startPage() {
  597.     var uri = Services.prefs.getComplexValue("browser.startup.homepage",
  598.                                              nsIPrefLocalizedString).data;
  599.     if (!uri) {
  600.       Services.prefs.clearUserPref("browser.startup.homepage");
  601.       uri = Services.prefs.getComplexValue("browser.startup.homepage",
  602.                                            nsIPrefLocalizedString).data;
  603.     }
  604.     return uri;
  605.   },
  606.  
  607.   mFeatures : null,
  608.  
  609.   getFeatures : function bch_features(cmdLine) {
  610.     if (this.mFeatures === null) {
  611.       this.mFeatures = "";
  612.  
  613.       try {
  614.         var width = cmdLine.handleFlagWithParam("width", false);
  615.         var height = cmdLine.handleFlagWithParam("height", false);
  616.  
  617.         if (width)
  618.           this.mFeatures += ",width=" + width;
  619.         if (height)
  620.           this.mFeatures += ",height=" + height;
  621.       }
  622.       catch (e) {
  623.       }
  624.     }
  625.  
  626.     return this.mFeatures;
  627.   },
  628.  
  629.   /* nsIContentHandler */
  630.  
  631.   handleContent : function bch_handleContent(contentType, context, request) {
  632.     try {
  633.       var webNavInfo = Components.classes["@mozilla.org/webnavigation-info;1"]
  634.                                  .getService(nsIWebNavigationInfo);
  635.       if (!webNavInfo.isTypeSupported(contentType, null)) {
  636.         throw NS_ERROR_WONT_HANDLE_CONTENT;
  637.       }
  638.     } catch (e) {
  639.       throw NS_ERROR_WONT_HANDLE_CONTENT;
  640.     }
  641.  
  642.     request.QueryInterface(nsIChannel);
  643.     handURIToExistingBrowser(request.URI,
  644.       nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW, null);
  645.     request.cancel(NS_BINDING_ABORTED);
  646.   },
  647.  
  648.   /* nsICommandLineValidator */
  649.   validate : function bch_validate(cmdLine) {
  650.     // Other handlers may use osint so only handle the osint flag if the url
  651.     // flag is also present and the command line is valid.
  652.     var osintFlagIdx = cmdLine.findFlag("osint", false);
  653.     var urlFlagIdx = cmdLine.findFlag("url", false);
  654.     if (urlFlagIdx > -1 && (osintFlagIdx > -1 ||
  655.         cmdLine.state == nsICommandLine.STATE_REMOTE_EXPLICIT)) {
  656.       var urlParam = cmdLine.getArgument(urlFlagIdx + 1);
  657.       if (cmdLine.length != urlFlagIdx + 2 || /firefoxurl:/.test(urlParam))
  658.         throw NS_ERROR_ABORT;
  659.       cmdLine.handleFlag("osint", false)
  660.     }
  661.   },
  662. };
  663. var gBrowserContentHandler = new nsBrowserContentHandler();
  664.  
  665. function handURIToExistingBrowser(uri, location, cmdLine)
  666. {
  667.   if (!shouldLoadURI(uri))
  668.     return;
  669.  
  670.   var navWin = getMostRecentBrowserWindow();
  671.   if (!navWin) {
  672.     // if we couldn't load it in an existing window, open a new one
  673.     openWindow(null, gBrowserContentHandler.chromeURL, "_blank",
  674.                "chrome,dialog=no,all" + gBrowserContentHandler.getFeatures(cmdLine),
  675.                uri.spec);
  676.     return;
  677.   }
  678.  
  679.   var navNav = navWin.QueryInterface(nsIInterfaceRequestor)
  680.                      .getInterface(nsIWebNavigation);
  681.   var rootItem = navNav.QueryInterface(nsIDocShellTreeItem).rootTreeItem;
  682.   var rootWin = rootItem.QueryInterface(nsIInterfaceRequestor)
  683.                         .getInterface(nsIDOMWindow);
  684.   var bwin = rootWin.QueryInterface(nsIDOMChromeWindow).browserDOMWindow;
  685.   bwin.openURI(uri, null, location,
  686.                nsIBrowserDOMWindow.OPEN_EXTERNAL);
  687. }
  688.  
  689. function nsDefaultCommandLineHandler() {
  690. }
  691.  
  692. nsDefaultCommandLineHandler.prototype = {
  693.   classID: Components.ID("{47cd0651-b1be-4a0f-b5c4-10e5a573ef71}"),
  694.  
  695.   /* nsISupports */
  696.   QueryInterface : function dch_QI(iid) {
  697.     if (!iid.equals(nsISupports) &&
  698.         !iid.equals(nsICommandLineHandler))
  699.       throw Components.results.NS_ERROR_NO_INTERFACE;
  700.  
  701.     return this;
  702.   },
  703.  
  704.   // List of uri's that were passed via the command line without the app
  705.   // running and have already been handled. This is compared against uri's
  706.   // opened using DDE on Win32 so we only open one of the requests.
  707.   _handledURIs: [ ],
  708. //@line 761 "/tmp/buildd/iceweasel-10.0.12esr/browser/components/nsBrowserContentHandler.js"
  709.  
  710.   /* nsICommandLineHandler */
  711.   handle : function dch_handle(cmdLine) {
  712.     var urilist = [];
  713.  
  714. //@line 787 "/tmp/buildd/iceweasel-10.0.12esr/browser/components/nsBrowserContentHandler.js"
  715.  
  716.     try {
  717.       var ar;
  718.       while ((ar = cmdLine.handleFlagWithParam("url", false))) {
  719.         var found = false;
  720.         var uri = resolveURIInternal(cmdLine, ar);
  721.         // count will never be greater than zero except on Win32.
  722.         var count = this._handledURIs.length;
  723.         for (var i = 0; i < count; ++i) {
  724.           if (this._handledURIs[i].spec == uri.spec) {
  725.             this._handledURIs.splice(i, 1);
  726.             found = true;
  727.             cmdLine.preventDefault = true;
  728.             break;
  729.           }
  730.         }
  731.         if (!found) {
  732.           urilist.push(uri);
  733.           // The requestpending command line flag is only used on Win32.
  734.           if (cmdLine.handleFlag("requestpending", false) &&
  735.               cmdLine.state == nsICommandLine.STATE_INITIAL_LAUNCH)
  736.             this._handledURIs.push(uri)
  737.         }
  738.       }
  739.     }
  740.     catch (e) {
  741.       Components.utils.reportError(e);
  742.     }
  743.  
  744.     count = cmdLine.length;
  745.  
  746.     for (i = 0; i < count; ++i) {
  747.       var curarg = cmdLine.getArgument(i);
  748.       if (curarg.match(/^-/)) {
  749.         Components.utils.reportError("Warning: unrecognized command line flag " + curarg + "\n");
  750.         // To emulate the pre-nsICommandLine behavior, we ignore
  751.         // the argument after an unrecognized flag.
  752.         ++i;
  753.       } else {
  754.         try {
  755.           urilist.push(resolveURIInternal(cmdLine, curarg));
  756.         }
  757.         catch (e) {
  758.           Components.utils.reportError("Error opening URI '" + curarg + "' from the command line: " + e + "\n");
  759.         }
  760.       }
  761.     }
  762.  
  763.     if (urilist.length) {
  764.       if (cmdLine.state != nsICommandLine.STATE_INITIAL_LAUNCH &&
  765.           urilist.length == 1) {
  766.         // Try to find an existing window and load our URI into the
  767.         // current tab, new tab, or new window as prefs determine.
  768.         try {
  769.           handURIToExistingBrowser(urilist[0], nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW, cmdLine);
  770.           return;
  771.         }
  772.         catch (e) {
  773.         }
  774.       }
  775.  
  776.       var URLlist = urilist.filter(shouldLoadURI).map(function (u) u.spec);
  777.       if (URLlist.length) {
  778.         openWindow(null, gBrowserContentHandler.chromeURL, "_blank",
  779.                    "chrome,dialog=no,all" + gBrowserContentHandler.getFeatures(cmdLine),
  780.                    URLlist);
  781.       }
  782.  
  783.     }
  784.     else if (!cmdLine.preventDefault) {
  785.       // Passing defaultArgs, so use NO_EXTERNAL_URIS
  786.       openWindow(null, gBrowserContentHandler.chromeURL, "_blank",
  787.                  "chrome,dialog=no,all" + gBrowserContentHandler.getFeatures(cmdLine),
  788.                  gBrowserContentHandler.defaultArgs, NO_EXTERNAL_URIS);
  789.     }
  790.   },
  791.  
  792.   helpInfo : "",
  793. };
  794.  
  795. let AboutHomeUtils = {
  796.   SNIPPETS_URL_PREF: "browser.aboutHomeSnippets.updateUrl",
  797.   get _storage() {
  798.     let aboutHomeURI = Services.io.newURI("moz-safe-about:home", null, null);
  799.     let principal = Components.classes["@mozilla.org/scriptsecuritymanager;1"].
  800.                     getService(Components.interfaces.nsIScriptSecurityManager).
  801.                     getCodebasePrincipal(aboutHomeURI);
  802.     let dsm = Components.classes["@mozilla.org/dom/storagemanager;1"].
  803.               getService(Components.interfaces.nsIDOMStorageManager);
  804.     return dsm.getLocalStorageForPrincipal(principal, "");
  805.   },
  806.  
  807.   loadDefaultSearchEngine: function AHU_loadDefaultSearchEngine()
  808.   {
  809.     let defaultEngine = Services.search.originalDefaultEngine;
  810.     let submission = defaultEngine.getSubmission("_searchTerms_");
  811.     if (submission.postData)
  812.       throw new Error("Home page does not support POST search engines.");
  813.     let engine = {
  814.       name: defaultEngine.name
  815.     , searchUrl: submission.uri.spec
  816.     }
  817.     this._storage.setItem("search-engine", JSON.stringify(engine));
  818.   },
  819.  
  820.   loadSnippetsURL: function AHU_loadSnippetsURL()
  821.   {
  822.     const STARTPAGE_VERSION = 1;
  823.     let updateURL = Services.prefs
  824.                             .getCharPref(this.SNIPPETS_URL_PREF)
  825.                             .replace("%STARTPAGE_VERSION%", STARTPAGE_VERSION);
  826.     updateURL = Services.urlFormatter.formatURL(updateURL);
  827.     this._storage.setItem("snippets-update-url", updateURL);
  828.   },
  829. };
  830.  
  831. var components = [nsBrowserContentHandler, nsDefaultCommandLineHandler];
  832. var NSGetFactory = XPCOMUtils.generateNSGetFactory(components);
  833.